﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace com.crawler.common
{
    public class CrawlerUtil
    {
        /// <summary>
        /// 判断是否是相同的章节
        /// </summary>
        /// <param name="i_chapter_source"></param>
        /// <param name="i_chapter_target"></param>
        /// <returns></returns>
        public static int GetChapterIndex(string i_chapter_name, List<ChapterInfo> i_chapter_list)
        {
            string t_real_chapter_name = i_chapter_name;

            if (string.IsNullOrEmpty(i_chapter_name) || string.IsNullOrEmpty(i_chapter_name))
                return 0;
           if(i_chapter_list==null || i_chapter_list.Count<=0)
                return 0;

            //去掉空格
           i_chapter_name = Regex.Replace(i_chapter_name, "\\s", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
           i_chapter_name = Regex.Replace(i_chapter_name, "[【】（）a-zA-Z，！？(),!?;\\.；、……]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

           string t_chapter_name = string.Empty;
            int t_chapter_index=0;
           for (int index = i_chapter_list.Count-1; index > 0; index--)
           {
               t_chapter_name = i_chapter_list[index].ChapterName;
               t_chapter_name = Regex.Replace(t_chapter_name, "\\s", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
               t_chapter_name = Regex.Replace(t_chapter_name, "[【】（）a-zA-Z，！？(),!?;；\\.、……]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

               if (i_chapter_name.Contains(t_chapter_name) || t_chapter_name.Contains(i_chapter_name))
               {
                   t_chapter_index = index;
                   break;
               }

           }
            //如果更新的章节多余10个章节，重新比较规则
           if (i_chapter_list.Count - t_chapter_index > 10)
           {
               t_chapter_index = 0;
               for (int index = i_chapter_list.Count - 1; index > 0; index--)
               {
                   if (t_real_chapter_name==i_chapter_list[index].ChapterName)
                   {
                       t_chapter_index = index;
                       break;
                   }
               }

               if (i_chapter_list.Count - t_chapter_index > 10)
                   t_chapter_index = 0;
           }
           return t_chapter_index;
        }

        public static bool CompareChaperName(string i_chapter_source_name, string i_chapter_target_name)
        {
            i_chapter_source_name = Regex.Replace(i_chapter_source_name, "\\s", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_chapter_source_name = Regex.Replace(i_chapter_source_name, "[【】（）a-zA-Z，！？(),!?;\\.；、……]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_chapter_target_name = Regex.Replace(i_chapter_target_name, "\\s", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_chapter_target_name = Regex.Replace(i_chapter_target_name, "[【】（）a-zA-Z，！？(),!?;\\.；、……]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            if (i_chapter_source_name.Contains(i_chapter_target_name) || i_chapter_target_name.Contains(i_chapter_source_name))
                return true;

            return false;
        }

        /// <summary>
        /// 获取站点完整链接地址
        /// </summary>
        /// <param name="i_site_url"></param>
        /// <param name="i_part_url"></param>
        /// <returns></returns>
        public static string GetFullUrl(string i_site_url, string i_part_url)
        {
            if (i_site_url.IndexOf("#") > -1 || i_part_url.IndexOf("#") > -1)
                return string.Empty;

            if (i_part_url.IndexOf("http://") > -1)
                return i_part_url;



            if (!i_part_url.StartsWith("http") && !i_part_url.StartsWith("/"))
            {
                i_part_url = i_site_url.Substring(0, i_site_url.LastIndexOf("/") + 1) + i_part_url;
            }

            if (i_part_url.StartsWith("/"))
            {
                Uri t_url = new Uri(i_site_url);

                i_part_url = "http://" + t_url.Host + i_part_url;
            }
            return i_part_url;

        }

        /// <summary>
        /// 过滤页面内容的换行标记
        /// </summary>
        /// <param name="i_html"></param>
        /// <returns></returns>
        public static string ReplacePageHtml(string i_html)
        {
            if (!string.IsNullOrEmpty(i_html))
            {
                i_html = i_html.Replace("\r", "").Replace("\n", "").Replace("\r\n", "");
            }

            return i_html;
        }

        /// <summary>
        /// 获取表的名字
        /// </summary>
        /// <param name="i_url"></param>
        /// <returns></returns>
        public static string GetTableName(string i_url)
        {
            Uri t_url = new Uri(i_url);

            string t_table_name = string.Empty;

            Match t_mc = Regex.Match(t_url.Host.ToString(), "\\S+\\.(\\S+)\\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (t_mc != null && t_mc.Groups.Count > 0)
            {
                 t_table_name = "tb" + t_mc.Groups[1].Value.ToString();
            }
            return t_table_name;
        }

        /// <summary>
        /// 过滤小说简介
        /// </summary>
        /// <param name="i_text"></param>
        /// <returns></returns>
        public static string FilterContent(string i_text)
        {
            if (string.IsNullOrEmpty(i_text))
                return string.Empty;
            i_text = Regex.Replace(i_text, "<br[/]*>", "q", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<.*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "q+", "<br>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = i_text.Replace("本文晋江独家发表谢绝转载", "");
            i_text = Regex.Replace(i_text, "<!--.*?-->", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "http.*?com", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<a.*?>.*?</a>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            i_text = Regex.Replace(i_text, "<script.*?>.*?</script>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<[/]*?iframe.*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<[/]*?table.*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<[/]*?tr[^<>]*>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<[/]*?td[^<>]*>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "<font[^<>]*>.*?</font>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "</font>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            i_text = Regex.Replace(i_text, "<span[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "</span>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?center[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?dd[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?dl[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?tbody[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?fieldset[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?script>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<h[^<>]*>.*?</h.*?>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<h[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?h[^<>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<[/]*?p[^<>]{3,}>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<img.*?>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "【.*?】", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "\\[.*\\]", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "（.*?）", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "{.*?}", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<b[^<>]*>.*?</b>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<ul[^<>]*>.*?</ul>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "<li[^<>]*>.*?</li>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = Regex.Replace(i_text, "'", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            i_text = GetGBString(i_text, 980);

            return i_text;
        }

        /// <summary>
        /// 截取字符串长度
        /// </summary>
        /// <param name="i_text"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static String GetGBString(string i_text, int length)
        {
            if (string.IsNullOrEmpty(i_text))
                return i_text;
            i_text = Regex.Replace(i_text, "<div[^<>]*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            i_text = Regex.Replace(i_text, "</div[^<>]*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            byte[] bytes = System.Text.Encoding.Unicode.GetBytes(i_text);
            if (bytes.Length <= length)
                return i_text;

            int n = 0;　//　表示当前的字节数
            int i = 0;　//　要截取的字节数
            for (; i < bytes.GetLength(0) && n < length; i++)
            {
                //　偶数位置，如0、2、4等，为UCS2编码中两个字节的第一个字节
                if (i % 2 == 0)
                {
                    n++;　　　//　在UCS2第一个字节时n加1
                }
                else
                {
                    //　当UCS2编码的第二个字节大于0时，该UCS2字符为汉字，一个汉字算两个字节
                    if (bytes[i] > 0)
                    {
                        n++;
                    }
                }
            }
            //　如果i为奇数时，处理成偶数
            if (i % 2 == 1)
            {
                //　该UCS2字符是汉字时，去掉这个截一半的汉字
                if (bytes[i] > 0)
                    i = i - 1;
                //　该UCS2字符是字母或数字，则保留该字符
                else
                    i = i + 1;
            }
            return System.Text.Encoding.Unicode.GetString(bytes, 0, i);
        }

    }
}
